package com.coffee.core.util;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * IP工具
 * @author Xulg
 */
public class IpUtils {

    public static String getIp(HttpServletRequest request) {
        // remoteIp
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0
                || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0
                || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (isEmpty(ip)) {
            ip = request.getHeader("Client_IP");
        }
        if (ip == null || ip.length() == 0
                || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                // 根据网卡取本机配置的IP
                InetAddress inetAddress;
                try {
                    inetAddress = InetAddress.getLocalHost();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                ip = inetAddress.getHostAddress();
            }
        }
        if (!isEmpty(ip)) {
            List<String> list = stringToList(ip, ",");
            if (list.size() > 1) {
                ip = list.get(0);
            }
        }
        return ip;
    }

    //private static String localIp;

    /**
     * 获取本地网络ip地址
     *
     * @return the ip address
     */
    @SuppressWarnings("all")
    public static String getLocalNetWorkIp() {
        String localIp;
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            // 遍历所有的网卡
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface element = netInterfaces.nextElement();
                // 如果是回环和虚拟网络地址的话继续
                if (element.isLoopback() || element.isVirtual()) {
                    continue;
                }
                Enumeration<InetAddress> addresss = element.getInetAddresses();
                while (addresss.hasMoreElements()) {
                    InetAddress address = addresss.nextElement();
                    // 这里暂时只获取ipv4地址
                    if (address instanceof Inet4Address) {
                        ip = address;
                        break;
                    }
                }
                if (ip != null) {
                    break;
                }
            }
            if (ip == null) {
                localIp = "127.0.0.1";
            } else {
                localIp = ip.getHostAddress();
            }
        } catch (Exception e) {
            localIp = "127.0.0.1";
        }
        return localIp;
    }

    @SuppressWarnings("all")
    private static List<String> stringToList(String str, String split) {
        List<String> list = new ArrayList<>();
        if (isEmpty(str)) {
            return list;
        }
        for (String s : str.split(split)) {
            if (isEmpty(s)) {
                continue;
            }
            list.add(s);
        }
        return list;
    }

    private static boolean isEmpty(String content) {
        if (content == null) {
            return false;
        }
        return content.trim().length() == 0;
    }
}
